package com.mall.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mall.pojo.Address;
import com.mall.service.IAddressService;
import com.mall.utils.R;
import com.mall.vo.AddressVo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收货地址管理
 *
 * @author cxx
 * @since 2023-05-02 22:43:57
 */

@RestController
@RequestMapping("/address")
@Validated
@SaCheckLogin
public class AddressController {
    @Resource
    private IAddressService addressService;

    /**
     * 用户新增收货地址
     *
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/6/10 10:23
     * @param    address    新增地址信息
     */
    @PostMapping("/add")

    public R<String> addAddress(@Valid @RequestBody AddressVo address) {
        Address addressDetails = new Address();
        BeanUtils.copyProperties(address, addressDetails);
        addressDetails.setUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        boolean save = addressService.save(addressDetails);
        return save ? R.success("添加地址成功 !") : R.error("添加地址失败 !");
    }

    /**
     * 获取某个用户所有的收货地址
     *
     * @param
     * @return com.mall.utils.R<java.util.List < com.mall.pojo.Address>>
     * @author cxx
     * @date 2023/5/6 10:11
     */
    @GetMapping("/get")

    public R<List<AddressVo>> getUserAllAddress() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
        List<Address> list = addressService.list(wrapper);
        List<AddressVo> voList = list.stream().map(item -> {
            AddressVo addressVo = new AddressVo();
            BeanUtils.copyProperties(item, addressVo);
            return addressVo;
        }).collect(Collectors.toList());
        return ObjectUtils.isEmpty(voList) ? R.error("该用户还没有设置地址!") : R.success(voList);
    }

    /**
     * 删除某个用户的某个收货地址
     *
     * @param id 收货地址记录的主键ID
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/5/6 10:16
     */
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean remove = addressService.remove(new LambdaQueryWrapper<Address>().eq(Address::getId, id).and(i -> i.eq(Address::getUserId, Long.valueOf(StpUtil.getLoginId().toString()))));
        return remove ? R.success("删除地址成功 !") : R.error("删除地址失败 !");
    }

    /**
     * 设置某个用户的默认收货地址
     *
     * @param id 收货地址记录的主键ID
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/5/6 10:21
     */
    @PutMapping("/default/{id}")
    public R<String> defaultAddress(@PathVariable Long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, Long.valueOf(StpUtil.getLoginId().toString())).and(i -> i.eq(Address::getIsDefault, 1));
        Address address = addressService.getOne(wrapper);
        if (ObjectUtils.isEmpty(address)) {
            boolean update = addressService.update(new LambdaUpdateWrapper<Address>().eq(Address::getId, id).and(i -> i.eq(Address::getUserId, Long.valueOf(StpUtil.getLoginId().toString()))).set(Address::getIsDefault, 1));
            return update ? R.success("设置默认地址成功!") : R.error("设置默认地址失败!");
        }
        LambdaUpdateWrapper<Address> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Address::getId, address.getId()).set(Address::getIsDefault, 0);
        addressService.update(updateWrapper);
        boolean update = addressService.update(new LambdaUpdateWrapper<Address>().eq(Address::getId, id).and(i -> i.eq(Address::getUserId, Long.valueOf(StpUtil.getLoginId().toString()))).set(Address::getIsDefault, 1));
        return update ? R.success("设置默认地址成功!") : R.error("设置默认地址失败!");

    }

}
